package com.kq.servlet.kqctrl;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.PreloadListener;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.utils.HtmlUtil;

public class get extends HttpServlet
{
  public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
  {
    response.setCharacterEncoding("UTF-8");
    String html = "";
    if (PreloadListener.loginInfo.isEmpty()) {
      html = "";
    }
    else {
      String name = PreloadListener.loginInfo.get("name").toString();
      String pswd = PreloadListener.loginInfo.get("pswd").toString();
      String userId = PreloadListener.loginInfo.get("id").toString();
      HtmlPage rootPage = PreloadListener.rootPage;
      String ajaxContent = "$.ajax({url:'http://erp.kanq.com.cn:806/pages/user_login.action',type:'get',dataType:'json',";
      ajaxContent = ajaxContent + "data:{'name':'" + name + "','pswd':'" + pswd + "'},async:false,cache:false,";
      ajaxContent = ajaxContent + "error: function(xhr){";
      ajaxContent = ajaxContent + "$.ajax({url:'http://erp.kanq.com.cn:806/pages/ajaxrequest.jsp',type:'post',dataType:'json',";
      ajaxContent = ajaxContent + "data:{'data':\"['list'.'a1','a2','a3','a4','a5','a6','a7','a8','a9','a10','a11','a12']=[# select id,contenttext,d_date,d_time,remark,userid,rzlb,bmid,oid,txrq,d_date2,projName from kanq_diary where userid=" + userId + " order by d_date desc ]\"},";
      ajaxContent = ajaxContent + "error: function(xhr,status,data){$('body').html(JSON.stringify(xhr.responseText)+'kqerptestbylin');}})}});";
      rootPage.executeJavaScript(ajaxContent);
      try {
        Thread.sleep(100L);
      }
      catch (InterruptedException e) {
        e.printStackTrace();
      }
      html = rootPage.getBody().getTextContent();
      int i = 0;
      while (html.indexOf("kqerptestbylin") == -1) {
        rootPage.executeJavaScript(ajaxContent);
        try {
          Thread.sleep(100L);
        }
        catch (InterruptedException e) {
          e.printStackTrace();
        }
        i++;
        System.out.println("执行了" + i + "次重试！");
        html = rootPage.getBody().getTextContent();
      }
    }

    System.out.println("html=" + html);
    HtmlUtil.writerJson(response, html);
  }
}